<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topbarwrap">
      <!-- 左侧标题 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" width="24px" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li></li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li
          class="login-run"
          v-if="isLogined"
          @click="$router.push('/user/cart')"
        >
          <img src="@/assets/img/cart.png" alt="" />
          购物车
          <span>{{ carTotal }}</span>
        </li>
        <li class="login-run" v-else @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeixinLoginApi } from "@/request/api.js";
export default {
  data() {
    return {};
  },
  methods: {
    ...mapMutations({
      // 方法名 ： "模块名/方法名"
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      isLoginedShow: "loginStatus/isLoginedShow",
      userInfoLogin: "userinfo/userInfoLogin",
      inituser: "userinfo/inituser",
    }),
    ...mapActions({
      asyncChangeIsShowLoginToast: "showToast/asyncChangeIsShowLoginToast",
      asyncuserInfoLogin: "userinfo/asyncuserInfoLogin",
    }),
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      carTotal: (state) => state.userinfo.carTotal,
      userInfo: (state) => state.userinfo.userInfo,
    }),
  },
  async created() {
    // console.log(this.$route.query.code);
    let code = this.$route.query.code;
    if (code) {
      // 微信扫码登录
      // 找后台去微信换token
      let res = await WeixinLoginApi({
        code,
      });
      // console.log("微信登录后res", res);
      // 返回code 中的三个结果
      // code == 0 成功
      if (res.code == 0) {
        this.asyncChangeIsShowLoginToast({
          isShow: true,
          Msg: "扫码登录成功",
          Type: "success",
        });
        // 2.存储token
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 3.更新右上角的购物车按钮
        this.isLoginedShow(true);
        // 4.清除地址栏中的code
        this.$route.query.code && this.$router.push(this.$route.path);
        // 更新vuex中的用户信息
        this.asyncuserInfoLogin();
      }
      // code == 400 二维码失效
      else if (res.code == 400) {
        // 2.1 提示重新扫码
        this.asyncChangeIsShowLoginToast({
          isShow: true,
          Msg: "请重新扫码",
          Type: "danger",
        });
        // 弹出模态框
        this.changeIsShowLoginModal(true);
      }
      // code == 407 绑定手机号
      else if (res.code == 407) {
        // 提示用户需要进行一次手机号
        this.asyncChangeIsShowLoginToast({
          isShow: true,
          Msg: "请使用使用手机号重新登陆",
          Type: "danger",
        });
        // 自动打开登录模块
        this.changeIsShowLoginModal(true);
        // 存储uuid(登陆用)
        localStorage.setItem("uuid", res.uuid);
      }
    } else {
      let token = localStorage.getItem("x-auth-token");
      this.isLoginedShow(Boolean(token));
      if (token) {
        this.asyncuserInfoLogin();
      } else {
        this.inituser();
      }
    }
  },
  // watch: {
  //   // 监听路由地址的变化
  //   "$route.path": {
  //     handler(newVal, oldVal) {
  //       console.log("watch...", newVal, oldVal);
  //       if (newVal != oldVal) {
  //         // 判断当前是否有token
  //         let token = localStorage.getItem("x-auth-token");
  //         // if (token) {
  //         //   this.isLoginedShow(true);
  //         // } else {
  //         //   this.isLoginedShow(false);
  //         // }
  //         this.isLoginedShow(Boolean(token));
  //       }
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
// scoped 只在当前组件有效
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  color: #fffefe;
  font-family: SourceHanSansSC;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    text-align: center;
    .right {
      li {
        float: left;
        margin-left: 20px;
        cursor: pointer;
        &.login-run {
          width: 124px;
          background: #0a328e;
        }
        img {
          vertical-align: middle;
          border-radius: 50%;
        }
      }
    }
  }
}
</style>